import { ref, computed } from "vue";
import { defineStore } from "pinia";

// export const useCounterStore = defineStore('counter', () => {
//   const count = ref(0)
//   const doubleCount = computed(() => count.value * 2)
//   function increment() {
//     count.value++
//   }

//   return { count, doubleCount, increment }
// })
export const useCounterStore = defineStore("counter", {
  state() {
    return {
      count: 0,
      success: true,
    };
  },
  getters: {
    doubleCount: (state) => state.count * 2,
    threeCount() {
      return this.doubleCount * 2 + this.count;
    },
    successGetter: (state) => !state.success,
  },
  actions: {
    increment() {
      this.count++;
      //   this.$patch(() => {
      // this.count++
      //   })
    },
    promiseEx() {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          if (this.success) {
            resolve("操作成功！");
          } else {
            reject(new Error("操作失败！"));
          }
        }, 1000); // 假设这个操作需要1秒钟来完成
      });
    },
  },
  debounce:{
    increment:3000
  }
});
